package gov.fnal.xpathsearch.rest;

import gov.fnal.xpath2ldap.XPathLDAPEvaluator;
import gov.fnal.xpath2ldap.XPathSearch;

import javax.ws.rs.*;

@Path("/xpathsearch")
public class XPathSearchResource {
  
   @GET
   @Path("/xmltest")
   @Produces("text/xml")
   public String getXPathExpression(@QueryParam("xpath") String expr) {
       System.out.println("xpath: " + expr);
       String respDoc = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
               "<company>" +
               "<compname>companyname</compname>" +
               "<contname>contactname</contname>" +
               "<address>address</address>" +
               "<city>city</city>" +
               "<country>country</country>" +
               "</company>";
       return respDoc;
   }

   @GET
   @Path("/help")
   @Produces("text/plain")
   public String getHelpMessage() {
       String helpmsg = "xpathsearch help message";
       System.out.println(helpmsg);
       return helpmsg;
   }
                                                
    @Consumes("text/xml")
    @POST
    @Produces("text/plain")
    //public String xpathExpression(String xml_in) {
    public String xpathExpression(XPathQuery xlq) {
        System.out.println(xlq + "\n");
        String cli_str = xlq.toCliParseable();
        System.out.println(cli_str + "\n");
        String[] inarr = xlq.toArray();
        //XPathLDAPEvaluator xpathSearcher = new XPathLDAPEvaluator(cli_str);
        
        //return cli_str + "\n";
        String result = XPathSearch.processXPathExpression(inarr);
        System.out.println(result);
        return result;
    }
    
}
